<template>
  <div class="register-page">
    <NavBar>
      <template #default> 新用户注册 </template>
    </NavBar>
    <div class="container">
      <!--form表单容器-->
      <div class="forms-container">
        <!--注册-->
        <el-form
          ref="formRef"
          :model="registerUser"
          label-width="80px"
          :rules="rules"
          class="loginForm"
        >
          <el-form-item label="用户名" prop="username">
            <el-input
              v-model="registerUser.username"
              placeholder="请输入用户名..."
            />
          </el-form-item>
          <el-form-item label="密&nbsp;&nbsp;&nbsp;码" prop="password">
            <el-input
              v-model="registerUser.password"
              type="password"
              placeholder="请输入密码..."
            />
          </el-form-item>
          <el-form-item label="确认密码" prop="password2">
            <el-input
              v-model="registerUser.password2"
              type="password"
              placeholder="请输入密码..."
            />
          </el-form-item>
          <el-form-item label="选择角色" prop="role">
            <el-select v-model="registerUser.role" placeholder="请选择角色...">
              <el-option label="管理员" value="admin" />
              <el-option label="用户" value="user" />
            </el-select>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" class="submit-btn" @click="onSubmit">
              注册
            </el-button>
          </el-form-item>
        </el-form>
        <div class="tiparea">
          <p>已有密码，<router-link to="/login"> 登录 </router-link></p>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import { reactive, toRefs, ref, nextTick } from "vue";
import { useRouter } from "vue-router";
import NavBar from "@/components/common/NavBar.vue";

export default {
  components: {
    NavBar,
  },
  setup() {
    // 自定义校验规则       当前验证规则的名称，当前验证规则的值，回调函数
    const validatePass = (rule, value, callback) => {
      if (value === "") {
        callback(new Error("请输入密码"));
      } else if (value !== state.registerUser.password) {
        callback(new Error("两次密码不一致"));
      } else {
        callback();
      }
    };

    const checkName = (rule, value, callback) => {
      // TODO: 利用axios 发送请求，判断用户名是否存在
      callback();
    };

    const state = reactive({
      registerUser: {
        username: "",
        password: "",
        password2: "",
        role: "",
      },
      flag: "true",
      rules: {
        username: [
          { required: true, message: "请输入用户名", trigger: "blur" },
          { min: 3, max: 10, message: "长度在3到10个字符", trigger: "blur" },
          { validator: checkName, trigger: "blur" }, // 绑定自定义校验规则
        ],
        password: [
          { required: true, message: "请输入密码", trigger: "blur" },
          { min: 6, max: 12, message: "长度在6到12个字符", trigger: "blur" },
        ],
        password2: [
          { required: true, message: "请输入密码", trigger: "blur" },
          { min: 6, max: 12, message: "长度在6到12个字符", trigger: "blur" },
          { validator: validatePass, trigger: "blur" }, // 绑定自定义校验规则
        ],
        role: [{ required: true, message: "请选择角色", trigger: "change" }],
      },
    });

    const router = useRouter();
    // const { ctx } = getCurrentInstance()
    const formRef = ref(null); // ref 是 Vue 3 的响应式 API，用于创建一个响应式引用。
    // 提交事件
    const onSubmit = () => {
      // ctx.refs[formName].validate((valid) => {
      // nextTick() 在 DOM 更新完成后执行回调，确保操作的是最新的 DOM
      nextTick(() => {
        if (!formRef.value) {
          console.log("formRef没有正确绑定");
          return;
        }
        formRef.value.validate((valid) => {
          if (!valid) {
            console.log("error submit!!");
            return false;
          }
          // TODO: 使用 axios 发送请求，注册用户
          console.log("注册成功");
          router.push({
            path: "/login",
          });
        });
      });
    };

    return {
      ...toRefs(state),
      onSubmit,
      formRef,
    };
  },
};
</script>

    <style scoped>
/* 页面背景 */
.register-page {
  display: flex;
  justify-content: center;
  align-items: center;
  min-height: 100vh; /* 保证页面至少占满一屏 */
  background-color: #f7f7f7;
  padding: 20px;
}

/* 主容器 */
.container {
  width: 100%;
  max-width: 400px; /* 限制最大宽度 */
  background-color: #fff;
  border-radius: 8px;
  box-shadow: 0 4px 10px rgba(0, 0, 0, 0.1); /* 增加阴影效果 */
  padding: 30px;
}

/* 表单容器 */
.forms-container {
  width: 100%;
}

/* 标题 */
.login-title {
  text-align: center;
  font-weight: bolder;
  margin-bottom: 30px;
  font-size: 1.5rem;
  color: #409eff;
}

/* 提交按钮 */
.submit-btn {
  width: 100%;
  height: 45px;
  font-size: 1.2rem;
}

/* 提示区 */
.tiparea {
  text-align: right;
  font-size: 12px;
  color: #666;
  margin-top: 20px;
}

.tiparea p a {
  color: #409eff;
  text-decoration: none;
}

.tiparea p a:hover {
  text-decoration: underline;
}

/* 输入框 */
.el-input {
  height: 40px;
  font-size: 1rem;
}

/* 表单项间距 */
.el-form-item {
  margin-bottom: 20px;
}
</style>
